<?php 

	/**
	 * @package Yomm
	 * @author bsuter
	 *
	 */
	Abstract Class Yomm_Module_Admin Extends Yomm_Controller
	{
		private $nav = null;
		
		public $cat_id = null;
		
		public $internal_nav = array();
		
		public function __construct()
		{
			parent::__construct();
			
			$this->nav = $this->buildNavigation();
			
			if(isset($this->get->cat))
			{
				$this->cat_id = $this->db->select("cms_cat", "id", "WHERE rewrite_link='{$this->get->cat}'", false)->id;
			} else {
				$this->cat_id = SiteConfig::DefaultCat;
			}
			
			$this->internal_nav = $this->internal_nav($this->session->user->id);
			
			$this->view->assign(array(
				'startup_console' 	=> $this->check_system(),
				'internal_nav' 		=> $this->internal_nav
			));
			
			/* insert css */
			$this->view->addCss($this->theme_full . 'css/layout-min.css'); /* TODO: always change to min */
			$this->view->addCss($this->theme_full . 'css/calendarview-min.css');
			$this->view->addCss($this->theme_full . 'css/facebox-min.css');
			
			/* insert javascript */
			
			$this->view->addJs($this->theme_full . 'js/backend-min.js'); /* TODO: always change to min */
			$this->view->addJs($this->theme_full . 'js/devmod-min.js'); /* TODO: always change to min */
			$this->view->addJs($this->theme_full . 'js/facebox-min.js');
			$this->view->addJs($this->theme_full . 'js/calendarview-min.js');
			$this->view->addJs(SiteConfig::Domain . 'share/js/scriptaculous.js');
			$this->view->addJs(SiteConfig::Domain . 'share/js/tabber.js');
			$this->view->addJs(SiteConfig::Domain . 'share/js/curvycorners.js');
			$this->view->addJs($this->theme_full . 'js/tiny_mce/tiny_mce.js');
			$this->view->addJs($this->theme_full . 'editarea/edit_area_full.js');
		}
		
		Public Function package_manager()
		{
			$pkg = new Yomm_Package_Manager();
			
			$myList = array();
			
			foreach($pkg->getList() as $item)
			{
				$data = $this->db->select("admin_pkg_installed", array("id", "pkg_version"), "WHERE pkg_id='{$item['id']}'", false);
				
				if(count($data) == 1)
				{
					$update = version_compare($item['version'], $data->pkg_version);
				} else {
					$update = 0;
				}
				
				$myList[] = array(
					'pkg' => $item,
					'installed' => count($data),
					'update' => $update
				);
			}
			
			$this->view->assign('list', $myList);
			
			return $this->view->fetch('admin/package_manager.tpl');
		}
		
		/**
		 * @param $param->pkg_id [int] paackage in wsdl to install
		 */
		public function package_install()
		{
			$pkg = new Yomm_Package_Manager();
			
			$item = $pkg->getPackage($this->getVar('pkg_id', GET, 0));
			
			$exe = new Yomm_Package_Install($item, $this->db);
			
			if(!$exe->installPackage())
			{
				new Yomm_Error(print_r($exe->getError()), true);
				return "<pre>" . print_r($exe->getError(), true) . "</pre>";
			}
			
			// admin_pkg_installed
			$data = $this->db->select("admin_pkg_installed", "id", "WHERE pkg_id='{$this->getVar('pkg_id', GET, 0)}'", false);
				
			if(count($data) == 1)
			{
				// existing just update
				$this->db->handler("admin_pkg_installed", array(
					"pkg_version" => $item['version']
				), "WHERE pkg_id='{$item['id']}'");
			} else {
				// creat new eentry
				$this->db->handler("admin_pkg_installed", array(
					"pkg_id" => $item['id'],
					"pkg_version" => $item['version']
				));
			}
			
			return "the package was successfully installed!";
		}
		
		/**
		 * @param $this->get->nav_id
		 */
		Public Function page_edit()
		{
			
			$lang = $this->db->select("cms_language", "*", false, true);
			
			$edits = array();
			
			foreach($lang as $key => $obj)
			{
				$edits[] = array(
					'lang' => $obj,
					'block' => $this->db->select("cms_block", "*", "WHERE nav_id='{$this->get->nav_id}' AND language_id='{$obj->id}'", false)
				);
			}
			
			$nav_obj = $this->db->select("cms_nav", "*", "WHERE id='{$this->get->nav_id}'", false);
			
			$this->view->assign(array(
				'edits' => $edits,
				'cats' => $this->db->select("cms_cat", "*", false, true),
				'nav_obj' => $nav_obj,
				'nav_obj_cat_rw' => $this->db->select("cms_cat", "rewrite_link", "WHERE id='{$nav_obj->cat}'", false)->rewrite_link,
				'env_vars' => $this->db->select("cms_env", array("COUNT(*)" => "count"), false, false)
			));
			return $this->view->fetch('admin/page_edit.tpl');
		}
		
		/**
		 * inline ajax call method: page_mgr
		 * additional exec: GET
		 * 
		 * in case of changing status:
		 * @param $this->get->nav_id [int] 
		 * @param $this->get->status [int] 0 = off; 1 = on
		 * 
		 * in case of deletion:
		 * @param $this->get->nav_id [int]
		 * @param $this->get->del [bool] true
		 */
		protected function page_mgr()
		{
			// set item to delete
			if(isset($this->get->nav_id) && isset($this->get->del) && $this->get->del == "true")
			{
				$handler = $this->db->handler("cms_nav", array("del" => 1), "WHERE id='{$this->get->nav_id}'");
			}

			// set an item to offline/online
			if(isset($this->get->nav_id) && isset($this->get->status))
			{
				$handler = $this->db->handler("cms_nav", array("status" => $this->get->status), "WHERE id='{$this->get->nav_id}'");
			}
			
			$cat_arr = array();
			
			foreach($this->db->select("cms_cat", "*", false, true) as $key => $value)
			{	
				$obj = new Yomm_Library_CmsNavigation(SiteConfig::DefaultLanguage, $value->id, true);
				$obj->setUserAdmin($this->session->user->id, $this->session->user->admin);
				$cat_arr[] = array('cat' => $value, 'nav' => $obj->getElements());
			}
			
			$this->view->assign('cat_list', $cat_arr);
			
			return $this->view->fetch('admin/page_mgr.tpl');
		}
		
		/**
		 * inline ajax call method: page_add 
		 * additional exec: POST
		 * 
		 * @param $this->post->isTranslation = This is an translation of an existing page. us this nav_id
		 * @param $this->post->cat_rw [char] reform this into id
		 */
		protected function page_add()
		{	
			if(isset($this->post->submit))
			{
				if(empty($this->post->title) || empty($this->post->rewrite_link) || empty($this->post->cat_rw) || empty($this->post->submit))
				{
					$this->view->assign('add_error', true);
					$this->set_user_history('missing informations for page_add, post values: ' . print_r($this->post, true));
				} else {
					
					// get the current cat_id based on the cat_rewrite name
					$cat_id = $this->db->select("cms_cat", "id", "WHERE rewrite_link='{$this->post->cat_rw}'", false)->id;
					
					// list the last sort number for this current cat
					$count_index = $this->db->select("cms_nav", "id", "WHERE cat='{$cat_id}'", true);
					
					// bug fix matnis: #0000018
					if(count($count_index) > 0)
					{
						$last_index = $this->db->select("cms_nav", "sort", "WHERE cat='{$cat_id}' ORDER by sort DESC LIMIT 1", false)->sort;
					} else {
						$last_index = 1;
					}
					
					$nav_id = $this->db->handler("cms_nav", array('parent' => $this->post->parent, 'status' => 0, 'user_id' => $this->session->user->id, 'cat' => $cat_id, 'sort' => ($last_index+1)), false, true);
					
					$block_id = $this->db->handler("cms_block", array('date' => mktime(), 'user_id' => $this->session->user->id, 'language_id' => $this->db->select("cms_language", "id", "WHERE i18n='".SiteConfig::DefaultLanguage."'", false)->id, 'title' => Yomm_Library_Convert::entitiesEncode($this->post->title), 'rewrite_link' => $this->post->rewrite_link, 'nav_id' => $nav_id), false, true);
					
					if($block_id) $this->view->assign('status', true);
					
					$this->set_user_history('added new page with following post values: ' . print_r($this->post, true));
				}
			}
			
			$this->view->assign('catas', $this->db->select("cms_cat", "*", false, true));
			
			return $this->view->fetch('admin/page_add.tpl');
		}
		
		/**
		 * inline ajax call method: devmod_add
		 * additional exec: POST & GET
		 * 
		 * get: edit a current dev mod
		 * post: save the informations from a new or already existing devmod
		 */
		protected function devmod_add()
		{
			if(isset($this->post->submit))
			{
				if(isset($this->post->edit_id))
				{
					// update
					$handler = $this->db->handler("dev_mods", array(
						"name" => $this->post->name,
						"theme" => $this->post->theme,
						"code" => addslashes($this->post->content),
						"dev_sql_id" => $this->post->devmod_table,
						"tpl_frontend_list" => $this->post->tpl_frontend_list,
						"tpl_frontend_add" => $this->post->tpl_frontend_add
					), "WHERE id='{$this->post->edit_id}'");

					if($handler)
					{
						return "<script language=\"javascript\">BkLoader.Get(\"id={$this->post->edit_id}\").Load('devmod_show');</script>";
					}
					
				} else {
					// new entry
					$id = $this->db->handler("dev_mods", array(
						"name" => $this->post->name,
						"theme" => $this->post->theme,
						"code" => addslashes($this->post->content),
						"dev_sql_id" => $this->post->devmod_table,
						"tpl_frontend_list" => $this->post->tpl_frontend_list,
						"tpl_frontend_add" => $this->post->tpl_frontend_add
					), false, true);
					
					// and now create a new nav block for this module!
					
					$nav_block = $this->db->handler("admin_nav", array(
						"internal_method" => "devmod_show",
						"title" => $this->post->name,
						"block_id" => 4,
						"extras" => "id=$id"
					));
					
					if($id)
					{
						return "<script language=\"javascript\">BkLoader.Get(\"id=$id\").Load('devmod_show');</script>";
					}
				}
			}
			
			$this->view->assign('tpls', Tools::ReaddirRecursive('theme/'.SiteConfig::ActiveTheme));
			
			if(isset($this->get->id))
			{
				$item = $this->db->select("dev_mods", "*", "WHERE id='{$this->get->id}'", false);

				$this->view->assign(array(
					'tpl_frontend_list' => $item->tpl_frontend_list,
					'tpl_frontend_add' => $item->tpl_frontend_add,
					'name' => $item->name,
					'code' => stripslashes($item->code),
					'sql_id' => $item->dev_sql_id,
					'theme_name' => $item->theme,
					'id' => $item->id
				));

			}
			$this->view->assign('sql_tables', $this->db->select("dev_sql", "*", false, true));
			return $this->view->fetch('admin/devmod_add.tpl');
		}
		
		/**
		 * inline ajax call method: devmod_export
		 * additional exec: POST & GET
		 * 
		 * get: generate an export for this GET ID
		 * post: submit a post to import an module
		 */
		public function devmod_export()
		{
			if(isset($this->get->id))
			{
				$gateway = new Yomm_Devmod_Export($this->db);
				
				$content = base64_encode($gateway->start($this->get->id));
				
				$_SESSION['header_name'] = $gateway->getName() . '.devmod';
				$_SESSION['header_content'] = $content;
				
				$this->view->assign(array(
					'export_done' => $gateway->getName(),
					'hash' =>  md5(strlen($content))
				));
				
			}
			
			$this->view->assign('mods', $this->db->select("dev_mods", "*", false, true));
			
			return $this->view->fetch('admin/devmod_export.tpl');
		}
		
		public function devmod_export_import_ajax()
		{
			if(isset($this->post->import))
			{
				$var = file_get_contents($_FILES['file']['tmp_name']);
				
				$gateway = new Yomm_Devmod_Import($this->db);
				
				$return = $gateway->start($var);
				
				$raw_log = $gateway->getLog();
				
				new Yomm_Error($raw_log); // its not really an error - but it should display in the log file
				
				$log = json_encode($raw_log);
				
				if($return == false)
				{
					return $this->ajaxResponse('<script language="javascript" type="text/javascript">window.top.window.FileUppDM.Stop(false, '.$log.')</script>', false);
				} else {
					return $this->ajaxResponse('<script language="javascript" type="text/javascript">window.top.window.FileUppDM.Stop(true, '.$log.')</script>', false);
				}
			}
		}
		
		/**
		 * inline ajax call method: devmod_show
		 * exec: GET
		 */
		public function devmod_show()
		{
			$item = $this->db->select("dev_mods", array(
				"dev_sql.name" => "sql_name",
				"dev_mods.theme" => "mod_theme",
				"dev_mods.code" => "code"
			), "LEFT JOIN(dev_sql) ON (dev_sql.id = dev_mods.dev_sql_id) WHERE dev_mods.id='{$this->get->id}'", false);
			
			if($item->mod_theme == '$this->theme')
			{
				$theme = $this->theme;
			} else {
				$theme = $item->mod_theme;
			}
			
			$mod = new Yomm_Devmod($item->sql_name, $theme);
			
			eval(stripslashes($item->code));
			
			$this->view->assign('grid', $mod->start());
			$this->view->assign('mod', $item);
			
			unset($mod);
			
			return $this->view->fetch('admin/devmod_show.tpl');
		}
		
		public function tpl_mgr()
		{
			$this->view->addJs($this->theme_full . 'editarea/edit_area_full.js"');
			
			$this->view->assign('tpls', Tools::ReaddirRecursive('theme/'.SiteConfig::ActiveTheme));
			
			return $this->view->fetch('admin/tpl_mgr.tpl');
		}
		
		/**
		 * @param $this->post->file
		 */
		public function tpl_mgr_load_file_ajax()
		{
			@chmod($this->post->file, 0777);
			
			clearstatcache();
			
			if(!file_exists($this->post->file))
			{
				return $this->ajaxResponse(array("status" => false, "content" => "file does not exists"));
			}

			if(!is_readable($this->post->file))
			{
				return $this->ajaxResponse(array("status" => false, "content" => "file '{$this->post->file}' is not readable; perms = " . substr(sprintf('%o', fileperms($this->post->file)), -4) . "; set permissions over FTP!"));
			}
			
			if(!is_writable($this->post->file))
			{
				return $this->ajaxResponse(array("status" => false, "content" => "file '{$this->post->file}' is not writeable; perms = " . substr(sprintf('%o', fileperms($this->post->file)), -4) . "; set permissions over FTP!"));
			}
			
			$content = @file_get_contents($this->post->file);
			
			if(!$content)
			{
				return $this->ajaxResponse(array("status" => false, "content" => "error while trying to get file content"));
			} else {
				return $this->ajaxResponse(array("status" => true, "content" => $content));
			}
			
		}
		
		/**
		 * @param $this->post->file
		 * @param $this->post->content
		 */
		public function tpl_mgr_save_file_ajax()
		{
			clearstatcache();
			
			// check if its a new file or not
			$explode_new = explode("|", $this->post->file);
			
			if($explode_new[0] == "NEW")
			{
				//$path = 'theme/'.SiteConfig::ActiveTheme.'/'; // not tpl folder anymore
				$path = 'theme/__admin/';
				$this->post->file = $path . $explode_new[1];
				
				if(Tools::createFile($this->post->file, $this->post->content) === false)
				{
					return $this->ajaxResponse(array("status" => false, "content" => "$path kann nicht beschrieben werden!"));
				} else {
					return $this->ajaxResponse(array("status" => true, "content" => "Das neue File wurde erfolgreich gespeichert!"));
				}
			}
			
			if (is_writable($this->post->file)) {
			
			    if (!$handle = fopen($this->post->file, 'w+')) {
			          return $this->ajaxResponse(array("status" => false, "content" => "Cannot open file (}$this->post->file})"));
			    }
			
			    if (fwrite($handle, stripslashes($this->post->content)) === FALSE) {
			         return $this->ajaxResponse(array("status" => false, "content" => "Cannot write to file (}$this->post->file})"));
			    }
			
				return $this->ajaxResponse(array("status" => true, "content" => "Success, wrote content to file ({$this->post->file})"));
			
				fclose($handle);
			
			} else {
			    return $this->ajaxResponse(array("status" => false, "content" => "The file {$this->post->file} is not writable"));
			}
		}
		
		/**
		 * inline ajax call method: cat_mgr
		 * additional exec: NO
		 */
		protected function dashboard()
		{
			$this->view->assign(array(
				'history' => $this->db->select("admin_user_history", array("message", "time"), "WHERE user_id='{$this->session->user->id}' ORDER by time ASC LIMIT 10", true),
				'last_blocks' => $this->db->select("cms_block", array('date', 'title'), "WHERE user_id='{$this->session->user->id}' ORDER by date DESC LIMIT 10", true),
				'autor' => $this->db->select("cms_nav", array("title","date"), "LEFT JOIN(cms_block) ON (cms_block.nav_id = cms_nav.id) WHERE cms_nav.user_id='{$this->session->user->id}'", true)
			));
			return $this->view->fetch('admin/dashboard.tpl');
		}

		/**
		 * set environment variables 
		 */
		protected function env_vars()
		{
			$mod = new Yomm_Devmod("cms_env", $this->theme);
			
			$mod->type('list')->fields(array(
				'id' => 'ID',
				'title' => 'Titel',
				'var_name' => 'Variable',
				'default_value' => 'Variabel default Value'
			))->sql("ORDER by id ASC");
			
			$mod->type('add')->fields(array(
				'title' => 'Titel',
				'description' => 'Beschreibung',
				'var_name' => 'Variabel Name',
				'default_value' => 'Variabel default Value'
			))->textObserve(array(
				'var_name' => 'Keine Sonderzeichen, kein Abstand. Nur a-z, A-Z, 0-9 und _. Darf nicht: devmod, title oder content heissen.'
			));
			
			$mod->type('edit')->fields(array(
				'title' => 'Titel',
				'description' => 'Beschreibung',
				'var_name' => 'Variabel Name',
				'default_value' => 'Variabel default Value'
			))->textObserve(array(
				'var_name' => 'Keine Sonderzeichen, kein Abstand. Nur a-z, A-Z, 0-9 und _. Darf nicht: devmod, title oder content heissen.'
			))->operator(array(
				'param' => 'id',
				'icon' => 'img/edit.png'
			));
			
			$this->view->assign('grid', $mod->start());
			return $this->view->fetch('common/grid.tpl');
		}
		
		/**
		 * @param $this->get->block_id [int] 
		 */
		public function get_env_vars()
		{
			$block_values = array();
			foreach($this->db->select("cms_env", "*", false, true) as $item)
			{
				$blockv = $this->db->select("cms_env_values", "value", "WHERE cms_block_id='{$this->get->block_id}' AND cms_env_id='{$item->id}'", false);
				$block_values[] = array(
					'env_item' => $item,
					'env_value' => (isset($blockv->value)) ? $blockv->value : null
				);
			}
			
			$this->view->assign(array(
				'block_value' => $block_values,
				'block_id' => $this->get->block_id
			));
			return $this->view->fetch('admin/env_vars_form.tpl');
		}
		
		/**
		 * @param $this->get->block_id [int]
		 */
		public function set_env_vars()
		{
			$handler = $this->db->delete("cms_env_values", "cms_block_id", $this->get->block_id);
		
			foreach($this->post as $k => $v)
			{
				if(empty($v) || !is_numeric($k))
				{
					continue;
				}
				
				$handler = $this->db->handler("cms_env_values", array(
					"cms_block_id" => $this->get->block_id,
					"value" => $v,
					"cms_env_id" => $k
				));
				
			}
			
			return $this->ajaxResponse(array(
				"error" => false,
				"message" => "Umgebungs Variabeln wurden erfolgreich gespeichert!"
			));
		}
		
		/**
		 * inline ajax call method: profile_history
		 * additional exec: GET
		 * 
		 * get: $this->get->id show history depending on this id
		 */
		protected function profile_history()
		{
			$user_id = $this->get->id;
			
			$mod = new Yomm_Devmod('admin_user_history', $this->theme);
			
			$mod->type('list')->fields(array(
				'id' => 'Err Nr.',
				'time' => 'Datum',
				'message' => 'History'
			))->callFunction('time', array(
				'function' => 'date',
				'args' => array(
					'd.m.Y / H:i',
					'%%value%%'
				)
			))->sql('WHERE user_id=\''.$user_id.'\' ORDER by time DESC');
			
			$mod->type('del')->operator(array(
				'param' => 'id',
				'icon' => 'img/del.png'
			));
			
			$this->view->assign('grid', $mod->start());
			return $this->view->fetch('common/grid.tpl');
		}
		
		/**
		 * inline ajax call method: profile_level
		 * additional exec: GET
		 * 
		 * get: $this->get->id does return the to profiled user id
		 */
		protected function profile_level()
		{
			$user_id = $this->get->id;
			
			$user_rights = array();
			
			foreach($this->db->select("admin_rights_nav", array("admin_nav_id" => "nav_id"), "WHERE admin_user_id='$user_id'", true) as $k => $v)
			{
				$user_rights[] = $v->nav_id;
			}
			
			$this->view->assign(array(
				"is_admin" => $this->db->select("admin_user", "admin", "WHERE id='$user_id'", false)->admin,
				"all_rights" => $this->db->select("admin_nav", "*", false, true),
				"user_rights" => $user_rights,
				"user_id" => $this->get->id
			));
			
			return $this->view->fetch('admin/profile_level.tpl');
		}
		
		
		/**
		 * inline ajax call method: profile_edit
		 * additional exec: POST
		 * 
		 * post: change the current password
		 */
		protected function profile_edit()
		{
			if(isset($this->post->pass_save))
			{
				if(strlen($this->post->pass) < 5)
				{
					$this->view->assign('pass_err', 'Das Eingegeben Passwort ist zu kurz');		
				} else {
					if($this->post->pass != $this->post->passwd)
					{
						$this->view->assign('pass_err', 'Das Passwort stimmt nicht mit der Wiederholung &uuml;berein');	
					} else {
						$handler = $this->db->handler("admin_user", array(
							"pass" => md5($this->post->pass)
						), "WHERE id='{$this->session->user->id}'");
						
						if($handler)
						{
							$this->view->assign('pass_true', 'Dass Passwort wurde erfolgreich ge&auml;ndert');
						}
					}
				}
			}
			$this->view->assign('user_data', $this->db->select("admin_user", "*", "WHERE id='{$this->session->user->id}'", false));
			return $this->view->fetch('admin/profile_edit.tpl');
		}
				
		
		/**
		 * inline ajax call method: mysql_mgr
		 * additional exec: POST & GET
		 * 
		 * post: submit the formular data and assign the sql statement to the template
		 * get: mysql confirmation statement will save & exectue | alter existing table
		 */
		public function mysql_mgr()
		{
			if(isset($this->post->save))
			{
				$sql = 'CREATE TABLE `'.$this->post->table.'` (';
				
				foreach($this->post->fields as $key => $field)
				{
					$sql.= '`'. $field . '` ';
					switch($this->post->type[$key])
					{
						case "pk" :
							$sql.= 'INT NOT NULL AUTO_INCREMENT PRIMARY KEY';
						break;
						case "int" :
							$sql.= 'INT';
						break;
						case "varchar" :
							$sql.= 'VARCHAR (255)';
						break;
						case "text" :
							$sql.= 'TEXT';
						break;
					}
					$sql.= ' NOT NULL ';
					
					if(count($this->post->fields) != ($key+1))
					{
						$sql.= ',';
					}
				}
				
				$sql.= ') ENGINE = MYISAM;';
				
				$_SESSION['tmp_table'] = $this->post->table;
				$_SESSION['tmp_sql'] = $sql;
				
				$this->view->assign('sql', $sql);
			}
			
			if(isset($this->get->save) && $this->get->save == true)
			{
				$q = $this->db->query($this->session->tmp_sql);
				
				if($q)
				{
					$this->db->handler("dev_sql", array('name' => $this->session->tmp_table));
				}	
				
				unset($_SESSION['tmp_table']);
				unset($_SESSION['tmp_sql']);
			}
			
			if(isset($this->post->action) && $this->post->action == "alter_table")
			{
				$sql = "ALTER TABLE {$this->post->table} ADD {$this->post->alt_name} {$this->post->alt_type};";
				
				$q = $this->db->query($sql);
			}
			
			// list active user added tables
			
			$tables = $this->db->select("dev_sql", "*", false, true);
			
			$html_tables = array();
			
			foreach($tables as $key => $value)
			{
				$q = $this->db->query("SHOW COLUMNS FROM " . $value->name);
				
				while($data = mysqli_fetch_assoc($q))
				{
					$html_tables[$value->name][] = $data;
				}
			}
			$this->view->assign('tables', $html_tables);
			return $this->view->fetch('admin/mysql_mgr.tpl');
		}
		
		/**
		 * internal navigation table manager
		 */
		protected function inav_mgr()
		{
			$mod = new Yomm_Devmod('admin_nav', $this->theme);
			
			$mod->type('list')->fields(array(
				'id' => 'ID',
				'title' => 'Titel',
				'internal_method' => 'Methoden Namen',
				'block_id' => 'Block Zugehörigkeit',
				'extras' => 'Extra Parameter'
			))->sql('ORDER by block_id,id ASC');
			
			$mod->type('add')->fields(array(
				'title' => 'Titel',
				'internal_method' => 'Methoden Namen',
				'block_id' => 'Block Zugehörigkeit',
				'extras' => 'Extra Parameter'
			));
			
			$mod->type('del')->operator(array(
				'param' => 'id',
				'icon' => 'img/del.png'
			));
			
			$mod->type('edit')->fields(array(
				'title' => 'Titel',
				'internal_method' => 'Methoden Namen',
				'block_id' => 'Block Zugehörigkeit',
				'extras' => 'Extra Parameter'
			))->operator(array(
				'param' => 'id',
				'icon' => 'img/edit.png'
			));
			
			$this->view->assign('grid', $mod->start());
			return $this->view->fetch('common/grid.tpl');
		}
		
		/**
		 * devmod manager
		 */
		protected function devmod_mgr()
		{
			$mod = new Yomm_Devmod('dev_mods', $this->theme);
			
			$mod->type('list')->fields(array(
				'id' => 'ID',
				'name' => 'Name'
			));
			
			$mod->type('del')->operator(array(
				'param' => 'id',
				'icon' => 'img/del.png'
			));
			
			$this->view->assign('grid', $mod->start());
			return $this->view->fetch('common/grid.tpl');
		}
		
		/**
		 * inline ajax call method: cat_mgr
		 * additional exec: NO
		 */
		protected Function cat_mgr()
		{	
			$mod = new Yomm_Devmod('cms_cat', $this->theme);
			
			$mod->type('list')->fields(array(
				'id' => 'id',
				'title' => 'Titel',
				'rewrite_link' => 'Rewrite Link',
				'nav_default' => 'Standard Seite'
			))->castField('nav_default', 'sql_list', array(
				"table" => "cms_nav",
				"fields" => array("cms_nav.id" => "value", "title" => "title"),
				"where" => "LEFT JOIN(cms_block) ON (cms_block.nav_id=cms_nav.id) WHERE cms_block.language_id=1 AND cms_nav.del=0 AND cms_nav.id='%%nav_default%%'"
			));
			
			$mod->type('edit')->fields(array(
				'title' => 'Titel',
				'rewrite_link' => 'Rewrite Link',
				'nav_default' => 'Standard Include'
			))->operator(array(
				'param' => 'id',
				'icon' => 'img/edit.png'
			))->castField('nav_default', 'sql_dropdown', array(
				"table" => "cms_nav",
				"fields" => array("cms_nav.id" => "value", "title" => "title"),
				"where" => "LEFT JOIN(cms_block) ON (cms_block.nav_id=cms_nav.id) WHERE cms_block.language_id=1 AND cms_nav.del=0 AND cms_nav.cat='%%param%%'"
			));
			
			$mod->type('add')->fields(array(
				'title' => 'Titel',
				'rewrite_link' => 'Rewrite'
			));
			
			$mod->type('del')->operator(array(
				'param' => 'id',
				'icon' => 'img/del.png'
			));
			
			$this->view->assign('grid', $mod->start());
			return $this->view->fetch('common/grid.tpl');
		}
		
		Public Function user_mgr()
		{
			$mod = new Yomm_Devmod('admin_user', $this->theme);
			
			$mod->type('list')->fields(array(
				'id' => 'ID', 'firstname' => 'Vorname', 'lastname' => 'Nachname', 'mail' => 'E-Mail'
			))->sql("WHERE id > 1 ORDER by firstname,lastname, mail ASC")->itemOperator('Berechtigungen', array(
				'href' => '#',
				'onclick' => 'BkLoader.Get(\'&id=%%id%%\').Load(\'profile_level\');'
			))->itemOperator('Logs', array(
				'href' => '#',
				'onclick' => 'BkLoader.Get(\'&id=%%id%%\').Load(\'profile_history\');'
			));
			
			$mod->type('edit')->fields(array(
				'firstname' => 'Vorname', 'lastname' => 'Nachname', 'mail' => 'E-Mail'
			))->operator(array(
				'param' => 'id',
				'icon' => 'img/edit.png'
			));
			
			$mod->type('del')->operator(array(
				'param' => 'id',
				'icon' => 'img/del.png'
			));
			
			$mod->type('add')->fields(array(
				'firstname' => 'Vorname', 'lastname' => 'Nachname', 'mail' => 'E-Mail Adresse', 'pass' => 'Md5 Password'
			))->textObserve(array(
				'pass' => 'Das Password wird nach dem speichern verschlüsselt!'
			))->callFunction('pass', array(
				'function' => 'md5',
				'args' => array(
					'%%value%%'
				)
			))->mail( array (
				'subject' => 'AVANCE CMS Administrations Login',
				'to' => array(
					'%%mail%%' => '%%firstname%% %%lastname%%'
				)
			));
			
			//$mod->export_dev();
					
			$this->view->assign('grid', $mod->start());
			return $this->view->fetch('common/grid.tpl');
		}
		
		protected function logs_mgr()
		{
			$mod = new Yomm_Devmod('cms_error', $this->theme);
			
			$mod->type('list')->fields(array(
				'id' => 'ID',
				'date' => 'Datum',
				'debug' => 'Text'
			))->sql('ORDER by id DESC');
			
			$mod->type('edit')->fields(array(
				'session' => 'Session',
				'get' => 'Get',
				'post' => 'Post',
				'debug' => 'Debug',
				'object' => 'Error Object'
			))->operator(array(
				'param' => 'id',
				'icon' => 'img/edit.png'
			));
			
			$mod->type('del')->operator(array(
				'param' => 'id',
				'icon' => 'img/del.png'
			));
			
			$this->view->assign('grid', $mod->start());
			
			//print_r($mod);
			
			//print_r($this->session);
			
			return $this->view->fetch('common/grid.tpl');
		}
		
	
		public function lang_mgr()
		{
			$mod = new Yomm_Devmod('cms_language', $this->theme);
			$mod->type('list')->fields(array(
				'id' => 'ID',
				'i18n' => 'i18n Tag',
				'name' => 'Name'
			))->sql('WHERE id > 1');
			
			$mod->type('edit')->fields(array(
				'i18n' => 'i18n Tag',
				'name' => 'Name'
			))->operator(array(
				'param' => 'id',
				'icon' => 'img/edit.png'
			));
			
			$mod->type('add')->fields(array(
				'i18n' => 'i18n Tag',
				'name' => 'Name'
			));
			
			$mod->type('del')->operator(array(
				'param' => 'id',
				'icon' => 'img/del.png'
			));
			
			$this->view->assign('grid', $mod->start());
			return $this->view->fetch('common/grid.tpl');
		}
		
		
		
		/* SOME OTHER AJAXIFY CALLS */
		
		public function profile_level_ajax()
		{
			// delete existing stuff
			$handler = $this->db->delete("admin_rights_nav", "admin_user_id", $this->post->user_id);

			foreach($this->post->rights as $item)
			{
				$add = $this->db->handler("admin_rights_nav", array(
					"admin_nav_id" => $item, "admin_user_id" => $this->post->user_id
				));
			}
			
			return $this->ajaxResponse(array('status' => true, 'message' => 'erfolgreich gespeichert'));
		}
		
		/**
		 * @param $this->post->name
		 */
		public function check_rewrite()
		{
			$_regex_chk = preg_match('/^[a-zA-Z0-9\_\-]+$/', $this->post->name);
			
			if(!$_regex_chk)
			{
				return $this->ajaxResponse(array('free' => false));
			}
			
			$db = $this->db->select("cms_block", array("cms_block.id", "cms_nav.del"), "LEFT JOIN(cms_nav) ON (cms_nav.id = cms_block.nav_id) WHERE cms_block.rewrite_link='{$this->post->name}'", false);
			
			if(count($db) > 0)
			{
				return $this->ajaxResponse(array('free' => false));
			} else {
				return $this->ajaxResponse(array('free' => true));
			}
		}
		
		/**
		 * @param $this->post->cat_rw
		 */
		public function add_page_loadnav_ajax()
		{
			return $this->ajaxResponse($this->nav[$this->post->cat_rw]);
		}
		
		
		
		public function change_list()
		{
		
			$this->unsetVar('post', '_'); // google chromium fix
			
			foreach($this->post as $cat_name => $cat_values)
			{
				/* 
				 * find out the type of sortable change: cat or nav or sub nav etc.
				 */
				
				preg_match_all("/([a-zA-Z]+)\_([0-9]+)/", $cat_name, $result);
				
				$type = $result[1][0]; // cat; nav
				$number = $result[2][0]; // [0-9]+
				
				
				foreach($cat_values as $index => $id)
				{
					switch($type)
					{
						case "cat" :
							$handler = $this->db->handler("cms_nav", array(
								"sort" => $index
							), "WHERE id=$id");
							
							if(!$handler)
							{
								return $this->ajaxResponse(array(
									'status' => false,
									'message' => 'error while updating navigation sort table'
								));
							}
						break;
						
						case "sub" :
							$handler = $this->db->handler("cms_nav", array(
								"sort" => $index
							), "WHERE id=$id");
							
							if(!$handler)
							{
								return $this->ajaxResponse(array(
									'status' => false,
									'message' => 'error while updating navigation sort table'
								));
							}
						break;
						
						case "subsub" :
							$handler = $this->db->handler("cms_nav", array(
								"sort" => $index
							), "WHERE id=$id");
							
							if(!$handler)
							{
								return $this->ajaxResponse(array(
									'status' => false,
									'message' => 'error while updating navigation sort table'
								));
							}
						break;
					}
				}
				
				return $this->ajaxResponse(array(
					'status' => true,
					'message' => 'navigation table successfully updated'
				));
			}
		}
		
		/**
		 * Edit function, if this page does not exists. create it!
		 * 
		 * @param $this->post->nav_id
		 * @param $this->post->nav_id_get
		 * @param $this->post->lang_id
		 */
		Public Function edit_page_ajax()
		{
			$this->setSession('last_stemp', mktime());
			
			if(empty($this->post->block_title) || empty($this->post->block_text) || empty($this->post->block_rewrite_link))
			{
				return $this->ajaxResponse(array('type' => 'update', 'status' => 'false', 'message' => 'title, text or rewrite link is empty!'));
			}
			
			if(empty($this->post->nav_id_get) || empty($this->post->lang_id))
			{
				return $this->ajaxResponse(array('type' => 'update', 'status' => 'false', 'message' => 'hidden post values nav_id_get or lang_id is empty!'));
			}
			
			// is it new or not???
			if(empty($this->post->nav_id))
			{
				$block_id = $this->db->handler("cms_block", array(
					'nav_id' => $this->post->nav_id_get,
					'language_id' => $this->post->lang_id,
					'title' => Yomm_Library_Convert::entitiesEncode($this->post->block_title),
					'text' => addslashes($this->post->block_text),
					'rewrite_link' => $this->post->block_rewrite_link,
					'date' => mktime(), 
					'user_id' => $this->session->user->id
				));
				
				return $this->ajaxResponse(array(
					'type' => 'insert',
					'status' => 'true',
					'block' => $this->db->select("cms_block","*","WHERE id='{$block_id}'", false)
				));
				
			} else {	
				// get previos text content for version table cms_block_versions
				$old_obj = $this->db->select("cms_block", array("text","id"),  "WHERE nav_id='{$this->post->nav_id}' AND language_id='{$this->post->lang_id}'", false);
				
				$version = $this->db->handler("cms_block_versions", array(
					"text" => $old_obj->text,
					"block_id" => $old_obj->id,
					"date" => mktime(),
					"user_id" => $this->session->user->id
				));
				
				$handler = $this->db->handler("cms_block", array(
					'title' => Yomm_Library_Convert::entitiesEncode($this->post->block_title),
					'text' => addslashes($this->post->block_text),
					'rewrite_link' => $this->post->block_rewrite_link,
					'date' => mktime(), 
					'user_id' => $this->session->user->id
				), "WHERE nav_id='{$this->post->nav_id}' AND language_id='{$this->post->lang_id}'");
				
				return $this->ajaxResponse(array(
					'type' => 'update',
					'status' => 'true',
					'message' => 'null',
					'block' => $this->db->select("cms_block","*","WHERE nav_id='{$this->post->nav_id}' AND language_id='{$this->post->lang_id}'", false)
				));
			}
		}
		
		public function get_block_versions()
		{
			$block_id = $this->db->select("cms_block", "id",  "WHERE nav_id='{$this->post->nav_id}' AND language_id='{$this->post->lang_id}'", false)->id;
			
			$obj = $this->db->select("cms_block_versions", "*", "WHERE block_id='{$block_id}' ORDER by date DESC", true);
			
			$nums = count($obj);
			
			$obj = $this->db->select("cms_block_versions", "*", "WHERE block_id='{$block_id}' ORDER by date DESC LIMIT 10", true);

			$versions = array();
			
			foreach($obj as $k=>$v)
			{
				$_tmp = $this->db->select("admin_user", array("firstname", "lastname"), "WHERE id='{$v->user_id}'", false);
				
				if(empty($_tmp->firstname))
				{
					$name = "Administrator";
				} else {
					$name = "{$_tmp->firstname} {$_tmp->lastname}";
				}
				
				$versions[] = array(
					'id' => $v->id,
					'text' => $v->text,
					'date' => $v->date,
					'user_id' => $name,
					'block_id' => $v->block_id,
					'count' => str_pad($nums--,5,"0",STR_PAD_LEFT)
				);
			}
			
			$versions = Yomm_Library_Convert::arrayToObject($versions);
			
			$this->view->assign(array(
				'versions' => $versions, 
				'mce_id' => $this->post->mce_id
			));
			
			return $this->ajaxResponse($this->view->fetch('admin/page_edit_versions.tpl'), false);
		}
		
		/**
		 * @param mce_id [post]
		 * @param block_version_id [post]
		 */
		public function set_block_version()
		{
			$text = $this->db->select("cms_block_versions", "text", "WHERE id='{$this->post->block_version_id}'", false)->text;
			
			echo $text;
		}
		
		/**
		 * @param $this->post->nav_id
		 * @param $this->post->cat_id
		 */
		public function edit_prop_ajax()
		{
			$this->setSession('last_stemp', mktime());
			
			$cat = $this->db->select("cms_cat", "id", "WHERE rewrite_link='{$this->post->cat_id}'", false)->id;
			
			$handler = $this->db->handler("cms_nav", array(
				'cat' => $cat,
				'parent' => $this->post->parent
			), "WHERE id='{$this->post->nav_id}'");
			
			if($handler)
			{
				return $this->ajaxResponse(array("error" => false));
			} else {
				return $this->ajaxResponse(array("error" => true));
			}
		}
		
		
		/**
		 * @param $this->post->image_file [char] $_FILES['image_file']
		 * @param $this->post->nav_id [int] 
		 * @param $this->get->image_container [string]
		 */
		Public Function add_image_ajax()
		{
			$this->setSession('last_stemp', mktime());
			
			if(isset($this->post->image_submit))
			{
				$image = new Yomm_Library_Image();
				
				$file = $image->Upload($_FILES['image_file']);
				
				if($file == false)
				{
					return $this->ajaxResponse('<script language="javascript" type="text/javascript">window.top.window.ImageUpp.Stop(false, \''.$image->error.'\', 0, \''.$this->get->image_container.'\')</script>', false);
				} else {
					
					$handler = $this->db->handler("cms_image", array(
						"nav_id" => $this->post->nav_id, "image_id" => $file, "layout" => 1
					));
					
					return $this->ajaxResponse('<script language="javascript" type="text/javascript">window.top.window.ImageUpp.Stop(true, \'upload_success\', '.$this->post->nav_id.', \''.$this->get->image_container.'\')</script>', false);
				}
			}	
		}
		
		/**
		 * @param $this->post->image_file [char] $_FILES['image_file']
		 * @param $this->post->nav_id [int] 
		 */
		Public Function add_file_ajax()
		{
			$this->setSession('last_stemp', mktime());
			
			if(isset($this->post->file_submit))
			{
				$file = new Yomm_Library_FileHandler();
				
				$new_file = $file->UploadFile($_FILES['file']);
				
				if(!$new_file)
				{
					return $this->ajaxResponse('<script language="javascript" type="text/javascript">window.top.window.FileUpp.Stop(false, \''.$file->getError().'\', \''.$this->post->nav_id.'\')</script>', false);
				} else {
					
					$this->db->handler("cms_file", array(
						"nav_id" => $this->post->nav_id,
						"file_id" => $new_file
					));
					
					return $this->ajaxResponse('<script language="javascript" type="text/javascript">window.top.window.FileUpp.Stop(true, \'\', \''.$this->post->nav_id.'\')</script>', false);
				}
			}
		}
		
		/**
		 * load all images for this navigation entry
		 * @param $this->post->nav_id [int] navigation id to load
		 */
		Public Function list_image_ajax()
		{
			$images = $this->db->select("cms_image", "*", "LEFT JOIN (image_lib) ON (cms_image.image_id = image_lib.id) WHERE cms_image.nav_id = '{$this->post->nav_id}'", true);
			return $this->ajaxResponse($images);
		}
		
		/**
		 * @param $this->post->nav_id [int] navigation id to load
		 */
		Public Function list_file_ajax()
		{
			$return = array();
			$files = $this->db->select("cms_file", "file_id", "WHERE nav_id='{$this->post->nav_id}'", true);
			foreach($files as $k => $item)
			{
				$file = new Yomm_Library_FileHandler();
				$return[] = array(
					'icon' => $file->getFileIcon($item->file_id),
					'src' => $file->getFile($item->file_id),
					'name' => $file->getName($item->file_id),
					'id' => $item->file_id
				);
			}
			
			$this->view->assign('files', $return);
			
			return $this->ajaxResponse($this->view->fetch('admin/page_edit_files.tpl'), false);
		}
		
		/**
		 * delete inhaltsbilder trough ajax
		 * @param $this->post->bild_id [int] bild id to delete
		 */
		public function delete_image_ajax()
		{
			$handle = $this->db->delete('cms_image', 'image_id', $this->post->bild_id);
			
			if($handle)
			{
				return $this->ajaxResponse(array('status' => true));
			} else {
				return $this->ajaxResponse(array('status' => false));
			}
		}
		
		/**
		 * @param serialized object
		 */
		Public Function update_image_ajax()
		{
			$this->setSession('last_stemp', mktime());
			$handler = $this->db->handler("cms_image", array(
				"layout" => $this->post->float,
				"text" => $this->post->text
			), "WHERE image_id='{$this->post->image_id}'");
			
			if($handler)
			{
				return $this->ajaxResponse(array('status' => true, 'image_id' => $this->post->image_id));
			} else {
				new Yomm_Error('error while updating image details. post: ' . print_r($this->post, true));
				return $this->ajaxResponse(array('status' => false, 'image_id' => $this->post->image_id));
			}
		}
		
		/**
		 * @param block_id [int] current block id
		 * @param block_version_id [int] the to compare block version id
		 */
		public function compare_versions()
		{
			$block_txt = $this->db->select("cms_block", "text", "WHERE id='{$this->get->block_id}'", false)->text;
			
			$version_txt = $this->db->select("cms_block_versions", "text", "WHERE id='{$this->get->block_version_id}'", false)->text;
			
			$diff = new Yomm_Library_Diff;
			$text = $diff->inline(string_to_array(htmlentities(strip_tags($version_txt))),string_to_array(htmlentities(strip_tags($block_txt))),2);
			
			$this->view->assign(array(
				'text' => $text,
				'changes' => count($diff->changes),
				'mce_id' => $this->get->mce_id,
				'text_ver' => $version_txt,
				'block_version_id' => $this->get->block_version_id
			));
			
			return $this->view->fetch('admin/compare_version.tpl');
		}
		
		/**
		 * @param $this->post->nav_id int navigation_item id
		 */
		public function get_user_rights()
		{
			// all available users
			$users = $this->db->select("admin_user", array("id", "firstname", "lastname", "admin"), "ORDER by id ASC", true);
			// return the id of the user which created the nav item
			$creator = $this->db->select("cms_nav", "user_id", "WHERE id='{$this->post->nav_id}'", false)->user_id;
			$newusers = array();
			foreach($users as $key => $value)
			{
				if($creator == $value->id)
				{
					$value->admin = 1;
				}
				$newusers[] = array(
					'id' => $value->id,
					'firstname' => $value->firstname,
					'lastname' => $value->lastname,
					'admin' => $value->admin,
					'is_allowed' => $this->db->select("admin_user_nav", array("COUNT(*)" => "count"), "WHERE cms_nav_id='{$this->post->nav_id}' AND admin_user_id='{$value->id}'", false)->count
				);
			}
			
			$this->view->assign(array(
				'users' => $newusers,
				'nav_id' => $this->post->nav_id
			));
			
			return $this->view->fetch('admin/user_rights_table.tpl');
		}
		
		/**
		 * TODO: check in ajax if return value is true or false!
		 * @param $this->get->nav_id int
		 * @param $this->post array contains all to set values
		 */
		public function set_user_right()
		{
			$deletion = $this->db->delete("admin_user_nav", "cms_nav_id", $this->get->nav_id);
			
			foreach($this->post as $key => $value)
			{
				if(!is_numeric($value)) continue;
				
				$handler = $this->db->handler("admin_user_nav", array(
					'cms_nav_id' => $this->get->nav_id,
					'admin_user_id' => $value
				));
				
				if(!$handler)
					return $this->ajaxResponse(array("error" => true));
			}
			
			return $this->ajaxResponse(array("error" => false));
		}
		
		private function check_system()
		{
			$err = array();
			
			if(file_exists('sql'))
			{
				$err[] = 'Warning: Delete the "sql" folder!';
			}
			
			if(file_exists('install'))
			{
				$err[] = 'Warning: Delete the "install" folder!';
			}
			
			// return after checks
			if(count($err > 0))
			{
				$str = null;
				foreach($err as $msg)
				{
					$str.= "<p>". date("H:i:s") . " - $msg</p>";
				}
				return $str;
			}
		}
		
		/* list admin nav including rights management */
		protected function internal_nav($user_id)
		{
			$internal_nav = array();
			
			foreach($this->db->select("admin_nav_block", "*", false, true) as $item)
			{
				// check if this user is an administrator:
				if($this->db->select("admin_user", "admin", "WHERE id='{$user_id}'", false)->admin == 1)
				{
					/* ALL */
					$subs = $this->db->select("admin_nav", array("internal_method" => "method", "title", "extras", "id"), "WHERE admin_nav.block_id='{$item->id}' ORDER by title ASC", true);
				} else {
					/* only them u have the rights for */
					$subs = $this->db->select("admin_nav", array("internal_method" => "method", "title", "extras", "id"), "WHERE admin_nav.id IN (SELECT admin_nav_id FROM admin_rights_nav WHERE admin_user_id='{$user_id}') AND admin_nav.block_id='{$item->id}' ORDER by title ASC", true);	
				}
				
				if(count($subs) > 0)
				{
					$internal_nav[] = array('id' => $item->id, 'title' => $item->title, 'subs' => $subs);
				}
				
			}
			
			return $internal_nav;
		}
		
		protected function internalNavSingle($user_id, $method)
		{
			if($this->db->select("admin_user", "admin", "WHERE id='{$user_id}'", false)->admin == 1)
			{
				return true;
			}
			
			switch($method)
			{
				default:
					
					// check the main method actions
					$subs = $this->db->select("admin_nav", "id", "WHERE admin_nav.id IN (SELECT admin_nav_id FROM admin_rights_nav WHERE admin_user_id='{$user_id}') AND internal_method='{$method}'", true);	
					
					if(count($subs) == 1) return true;
					
				break;
				case "devmod_show":
					// TODO: check if this user really is allowed by selecting the right from USER-RIGHTS
					return true;
				break;
				case "dashboard":
					return true;
					
				break;
				case "page_edit":

					$try = $this->db->select("admin_user_nav", "id", "WHERE cms_nav_id='{$this->get->nav_id}' AND admin_user_id='{$user_id}'");
					
					if(count($try) == 1) return true;
					
					$try = $this->db->select("cms_nav", "id", "WHERE id='{$this->get->nav_id}' AND user_id='{$user_id}'");
					
					if(count($try) == 1) return true;
					
				break;
				
				case "profile_edit":
					return true;
				break;
				
				case "profile_level":
					return true;
				break;
			}
			return false;
		}
		
	/**
		 * this method will not be implemented into the framework for security reasons.
		 * Else you could open any method trhough this method and this Controller needs
		 * a login auth.
		 * 
		 * @param $this->get->name [string] contains the name of the method which should be requested.
		 */
		public function request_method()
		{	
			$this->setSession('last_stemp', mktime());
			
			$error = false;
			
			if(!isset($this->get->name))
			{
				return exit;
			}
			// check if this has permission to load this function:
			
			if(!method_exists($this, $this->get->name))
			{
				$error = true; 
				$output = "method does not exists!";
			} else {
				$output = call_user_func_array(array($this, $this->get->name), array());
			}
			
			if(is_null($output) || empty($output))
			{
				$error = true;
				$output = "Error while loading '{$this->get->name}'";
			}
			
			if($this->internalNavSingle($this->session->user->id, $this->get->name) === false)
			{
				$error = true;
				$output = $this->local->admin_main_norights($this->get->name);
			}
			
			// find page title if available
			$obj = $this->db->select("admin_nav", "title", "WHERE internal_method='{$this->get->name}'", false);
			if(isset($obj->title))
			{
				$title = $obj->title;
			} else {
				$title = "Ihr Profil";
			}
			return $this->ajaxResponse(array(
				'error' => $error,
				'life_time' => mktime(),
				'page_title' => $title,
				'source' => $output,
			));
		}
		
		/**
		 * if the ajax return from request_method() does not have a valid form, javascript
		 * calls this function debug_method_content() - the only possibility to catch and 
		 * wrap php errors.
		 * 
		 * @param $this->post->response_text
		 */
		public function debug_method_content()
		{
			$encoded_response = htmlentities($this->trim_source($this->post->response_text));
			
			new Yomm_Error($encoded_response);
			
			return $this->ajaxResponse($encoded_response, false);
		}
		
		/**
		 * download the content of a file depending on the header
		 * 
		 * @param $this->session->header_name [string] the name of the file
		 * @param $this->session->header_content [string] the base64 encoded content of the to downloaded file
		 * 
		 * @param $this->get->hash [string] md5 of strlen of content
		 */
		public function download()
		{
			if(empty($this->session->header_name) || empty($this->session->header_content) || empty($this->get->hash))
			{
				$err = new Yomm_Error("error while download cause of empty header_name and empty header_content");
				return $err->getMessage();
				exit;
			}
			
			if(md5(strlen($this->session->header_content)) !== $this->get->hash)
			{
				$err = new Yomm_Error('export download hash does not match with session content');
				return $err->getMessage();
				exit;
			}
			
			header("Content-Type: text/plain; name={$this->session->header_name}");
			header("Content-Disposition: attachment; filename={$this->session->header_name}");
			
			echo base64_decode($this->session->header_content);
			
			unset($_SESSION['header_name'], $_SESSION['header_content']);
			
			exit;
		}
		
		/**
		 * check if the last action of the backend
		 */
		public function check_stemp()
		{
			$diff = mktime() - $this->session->last_stemp;
			if(($diff) > 900)
			{
				$this->set_user_history('the check_stemp is out of time!');
				// its bigger then 3600 (so your request was to long ago)
				return $this->ajaxResponse(array('is_valid' => false, 'stemp' => $this->session->last_stemp, 'mktime' => mktime(), 'diff' => $diff));
			} else {
				// everything is all right
				return $this->ajaxResponse(array('is_valid' => true, 'stemp' =>  $this->session->last_stemp, 'mktime' => mktime(), 'diff' => $diff));
			}
		}
		
		/**
		 * delete tabs and white spaces from the content.
		 * 
		 * @param $source [char] content to trim, delete tabs and spaces
		 */
		private function trim_source($source)
		{
			return str_replace(array("\t", "\n"), '', trim($source));
		}
		
		/**
		 * does save all informations which are sent to this method in the users history
		 * depending on the $session->user->id.
		 * 
		 * @param $this->post->message
		 * @return void
		 */
		public function async_history()
		{
			$this->set_user_history($this->post->message);
		}
		
		protected function set_user_history($message)
		{
			$this->db->handler("admin_user_history", array("user_id" => $this->session->user->id, "message" => $message, "time" => mktime()));
		}
	}